package cn.dgut.training.dto;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class GroupBuyingDTO extends BaseDTO{

    @ApiModelProperty(value = "团购活动表的ID")
    private Long id;

    @ApiModelProperty(value = "团购规则ID，关联litemall_groupon_rules表ID字段")
    private Long rulesId;

    @ApiModelProperty(value = "商品id")
    private Long goodsId;

    @ApiModelProperty(value = "商品名称")
    private String goodsName;

    @ApiModelProperty(value = "商品图片或者商品货品图片")
    private String picUrl;

    @ApiModelProperty(value = "优惠金额")
    private BigDecimal discount;

    @ApiModelProperty(value = "达到优惠条件的人数")
    private Integer discountMember;

    @ApiModelProperty(value = "参与人数")
    private long boudaryMember;

    @ApiModelProperty(value = "团购活动状态，开团则0，人数满足则1，团购超时则2，管理员手动下线则3")
    private Integer status;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "团购过期时间")
    private Date expireTime;

//    团购活动创建次数
    @ApiModelProperty(value = "创建次数")
    private long count;

    @ApiModelProperty(value = "货品id")
    private long productId;

    @ApiModelProperty(value = "商品规格值列表，采用JSON数组格式")
    private String specifications;

    @ApiModelProperty(value = "商品货品数量")
    private Integer productNumber;

    public static QueryWrapper getWrapper(GroupBuyingDTO groupBuyingDTO) {

        if (groupBuyingDTO.getOrderBy() == null) {
            groupBuyingDTO.setSort("desc");
        }
        if (groupBuyingDTO.getSort() == null) {
            groupBuyingDTO.setSort("add_time");
        }

        QueryWrapper queryWrapper = new QueryWrapper();

        if(groupBuyingDTO.getStatus() != null){
            queryWrapper.eq("status",groupBuyingDTO.getStatus());
        }

        if(groupBuyingDTO.getRulesId() != null){
            queryWrapper.eq("rules_id",groupBuyingDTO.getRulesId());
        }

        if(groupBuyingDTO.getId() != null){
            queryWrapper.eq("id",groupBuyingDTO.getId());
        }

        if (!StringUtils.isBlank(groupBuyingDTO.getOrderBy())) {
            if ("desc".equals(groupBuyingDTO.getSort())) {
                queryWrapper.orderByDesc(groupBuyingDTO.getOrderBy());
            } else {
                queryWrapper.orderByAsc(groupBuyingDTO.getOrderBy());
            }
        }
        return queryWrapper;
    }


}
